package com.xcity.game.combat.skill.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.game.combat.Action;
import com.xcity.game.combat.ActionEffect;
import com.xcity.game.combat.CombatUtils;
import com.xcity.game.combat.Round;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.buff.CombatBuff;
import com.xcity.game.combat.skill.CombatSkill;
import com.xcity.game.combat.unit.CombatUnit;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.skill.Skill;

/**
 * 以毒攻毒<br/>
 * 辣眼睛的舞姿对敌方全员造成每回合损失{0=10#0}%技能伤害的减益状态，该状态直到敌方有一人死亡才会结束
 * @author yang.li
 *
 */
public class COMBAT_SKILL_90101 extends CombatSkill {

	public COMBAT_SKILL_90101(Skill skill) {
		super(skill);
	}

	@Override
	public void calcDamage(Action action) {
		List<CombatUnit> units = action.target.all();
		for (int i = 0; i < units.size(); i++) {
			CombatUnit target = units.get(i);
			if (!target.isDied()) {
				ActionEffect effect = action.addTarget(target);
				target = effect.getUnit(); // 修正目标
//				int damage = CombatUtils.calcCommonMagicDamage(action.unit, target);
//				effect.effect(CombatAttributeType.HP, -damage);
				CombatUtils.calcMagicAttack(effect);
				// debuff
				action.addBuff(new BUFF(skill, target));
			}
		}
	}
	
	static class BUFF extends CombatBuff {
		float reduceMagicPowerRate;
		List<CombatUnit> aliveUnits;
		public BUFF(Skill skill, CombatUnit owner) {
			super(skill.getTemplate().getCombatBuff(), owner);
			reduceMagicPowerRate = skill.getParameter(0) / GameStaticConfig.percentBase;
			List<CombatUnit> units = owner.getTeam().all();
			aliveUnits = new ArrayList<CombatUnit>(units.size());
			for (int i = 0; i < units.size(); i++) {
				CombatUnit u = units.get(i);
				if (!u.isDied()) {
					aliveUnits.add(u);
				}
			}
		}
		@Override
		public int getId() {
			return 90101;
		}
		@Override
		public void roundStart(Round round) {
			active = true;
			int power = (int) owner.getMagicPower();
			power -= reduceMagicPowerRate * power;
			power = Math.max(power, 1);
			owner.setAttributeValue(CombatAttributeType.MAGIC_POWER, power);
		}
		@Override
		public void roundEnd(Round round) {
			if (state == STATE_ADDED && hasDead()) {
				uneffect();
				round.addBuff(this);
			}
		}
		private boolean hasDead() {
			List<CombatUnit> units = aliveUnits;
			if (units == null || units.isEmpty()) {
				return true;
			}
			for (int i = 0; i < units.size(); i++) {
				CombatUnit u = units.get(i);
				if (u.isDied()) {
					return true;
				}
			}
			return false;
		}
		@Override
		@JSONField(serialize = false)
		public boolean isDebuff() {
			return true;
		}
	}

}
